using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;
using Fieldbook.Common;

namespace Fieldbook.DAL
{
    public static class PlantSpecieDAO
    {
        #region select
        private static List<PlantSpecie> LoadPlantSpecies(SqlDatabase db, DbCommand command)
        {
            List<PlantSpecie> res = new List<PlantSpecie>();
            IDataReader reader = null;
            try
            {
                reader = db.ExecuteReader(command);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        PlantSpecie entity = new PlantSpecie();
                        entity.PlantSpecieID = reader.GetInt64(0);
                        entity.CategoryID = (PlantSpecie.PlantCategory)reader.GetInt32(1);
                        entity.Description = reader.GetString(2);
                        res.Add(entity);
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
                res = null;
                throw ex;
            }
            return res;
        }
        public static List<PlantSpecie> LoadAllPlantSpecies()
        {
            string selectSQL = @"SELECT PLANTSPECIEID, CATEGORYID, DESCRIPTION FROM dbo.PlantSpecie ORDER BY CATEGORYID ASC";
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = db.GetSqlStringCommand(selectSQL);
            return LoadPlantSpecies(db, cmd);
        }
        #endregion

        #region insert
        private static DbCommand GetInsertCommand(SqlDatabase db)
        {
            string insertSQL = @"INSERT INTO dbo.PlantSpecie (CATEGORYID, DESCRIPTION) 
                    VALUES (@CATEGORYID, @DESCRIPTION) SET @PLANTSPECIEID = SCOPE_IDENTITY()";
            DbCommand insertCommand = db.GetSqlStringCommand(insertSQL);
            db.AddOutParameter(insertCommand, "@PLANTSPECIEID", DbType.Int64, 8);
            db.AddInParameter(insertCommand, "@CATEGORYID", DbType.Int32);
            db.AddInParameter(insertCommand, "@DESCRIPTION", DbType.String);
            return insertCommand;
        }
        private static void SetInsertValues(SqlDatabase db, DbCommand cmd, PlantSpecie entity)
        {
            db.SetParameterValue(cmd, "@CATEGORYID", (int)entity.CategoryID);
            db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
        }
        public static bool InsertPlantSpecie(PlantSpecie entity)
        {
            bool res = false;
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
            if (res)
            {
                entity.PlantSpecieID = (long?)db.GetParameterValue(cmd, "@PLANTSPECIEID");
            }
            return res;
        }

        #endregion
    }
}
